package com.wutianyi.text;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.wltea.analyzer.dic.Dictionary;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.wutianyi.entity.EntityTypeEnum;

/**
 * @author hanjiewu 对字典的管理很维护
 */
public class DictionaryServices {

	private static final String EXT_WORDS = "extwords";
	// private static final String RULE_1 = "w_root_1";

	static {
		Dictionary.getInstance();
		List<Entity> extWords = DatastoreServiceFactory.getDatastoreService()
				.prepare(new Query(EntityTypeEnum.Extwords.name()))
				.asList(FetchOptions.Builder.withDefaults());
		List<String> words = new ArrayList<String>();
		for (Entity entity : extWords) {
			words.add(entity.getKey().getName());
		}
		Dictionary.loadExtendWords(words);
	}

	/**
	 * @param words
	 *            扩展词典
	 */
	public static void addExtendWords(Collection<String> words) {
		if (null == words || words.size() == 0) {
			return;
		}
		List<Entity> entities = new ArrayList<Entity>(words.size());
		for (String word : words) {
			Entity entity = new Entity(EXT_WORDS, word);
			entities.add(entity);
		}
		DatastoreServiceFactory.getDatastoreService().put(entities);
		Dictionary.loadExtendWords(words);
	}

	// private static void parseExtWords(Collection<String> words) {
	// IKSegmentation iksegment = null;
	// try {
	// for (String word : words) {
	// iksegment = new IKSegmentation(new StringReader(word));
	// Lexeme lexeme = iksegment.next();
	// while (null != lexeme) {
	//
	// }
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }

	// }
}
